﻿using Ekton_Manage_Framework.Methods.HttpSender;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using static Ekton_Manage_DTO.CommonModels.DtoFileModel;

namespace Ekton_Manage_Framework.Methods
{
    public class UpLoadFile
    {
        private readonly HttpRequestMethod _httpRequest;
        private readonly IConfiguration _configuration;
        private readonly SerializeHelper _serialize;
        public UpLoadFile(HttpRequestMethod httpRequest,
                      IConfiguration configuration,
                      SerializeHelper serialize)
        {
            _httpRequest = httpRequest;
            _configuration = configuration;
            _serialize = serialize;
        }

        /// <summary>
        /// 上传多个文件  
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<List<string>> UploadFile(FormData data)
        {
            var responseData = await _httpRequest.Post($"{_configuration["uploadFileUrl:baseUrl"]}{_configuration["uploadFileUrl:base64Files"]}", data.ToJson(), null);
            dynamic serializeData = await _serialize.Deserialize(responseData, null);
            return JsonConvert.DeserializeObject<List<string>>(serializeData.data.ToString());
        }

        /// <summary>
        /// 上传单个文件  
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> UploadFile(FileDataBase64 data)
        {
            var responseData = await _httpRequest.Post($"{_configuration["uploadFileUrl:baseUrl"]}{_configuration["uploadFileUrl:base64File"]}", data.ToJson(), null);
            dynamic serializeData = await _serialize.Deserialize(responseData, null);
            return serializeData.data.ToString();
        }
    }
}
